To find whether an attributeValue of 1 module exists in another module

Im new to DXlprogramming, Thanks to developerworks Forum. i get max help from it.

im trying to get the common attributeValues of an attribute of two modules. So, im checking whether an attributeValue of an attribute of first module is present in second module. Below is the two approaches i tried but could not succeed .pl help!

1)for o in m1 do {
eValue = o.attr
AttrDef ad = find(m2,attr)
if(attributeValue(ad,eValue)){ //do something } }

2)
eValue = o.attr
if(isMember(m2.attr,eValue)){ //do something }
PJAnurag - Wed Sep 12 02:14:23 EDT 2012

Re: To find whether an attributeValue of 1 module exists in another module
Peter_Albert - Wed Sep 12 04:43:55 EDT 2012

Welcome to this forum!

Let's first see why your approach failed: in the first attempt you used 'if(attributeValue(ad,eValue)){ //do something } }'. The DXL help reads for 'attributeValue': "Returns true if the specified string contains valid data for the specified attribute definition". So you did not check common attribute values, but you checked if the given attribute value from Module 1 would be a valid attribute value in Module 2. This is not what you want.

In the second try, you used 'isMember'. This is going into the right direction, but assumes that the attribute is defined to be of type multi-value enumeration in both Modules. And even if that is the case, it would fail if in the first Module two values are actually selected, as 'eValue = o.attr' in that case gives you a list of both values.

Let's keep it simple to start with, and only compare "simple" attribute values, i.e. we do not check whether any selection of a multi-value attribute is actually selected in the other Module. We just compare whether the overall value is the same.

In any case, you have to compare each Object in Module 2 with each Object in Module 1. However, if you do this with nested loops à la

for o1 in m1 do
{
  string eValue1 = o1.attr ""
  for o2 in m2 do
  {
    string eValue2 = o2.attr ""
    if (eValue1 == eValue2){print "\n" eValue1 " matches"}
  }
}

then you quickly run into problems as this script will require a very long time to run if the Modules are large enough. The solution to the problem is the use of Skip lists, such that you have to loop through each Module only once. Now, Skip lists might a bit heavy to start with if you are new to DXL, but here you go. A Skip list is a list of key/value pairs, and in the example below we use two Skip lists, one for each Module, where in each list the key is each attribute value appearing in the Module, and the value is a list of Objects in which the attribute value appears (plus a note if the attribute (also) is a Module level attribute).

The actual comparison is then very quick: For each key in the first list, check if the key exists in the second list, and if so, report the match. Here is the complete DXL:

pragma runLim, 0
// =============================================================================
Skip S_getAttValueList(Module M_mod, string s_attrName)
{ // Get a list of all attribute values in the Module
  // key  : string attribute value
  // value: comma-separated list of Object Absolute Numbers 
  //        and / or indication that it is a Module level attribute
  // Note : no specific treatment of multi-value enumeration-type attributes
  
  Skip    S_attValueList = null
  Object  O_obj
  AttrDef A_ad
  string  s_Key, s_Value
  A_ad = find(M_mod, s_attrName)
  if (!null A_ad)
  { // Attribute does not exist in Module
    S_attValueList = createString
    if (A_ad.object)
    { // Object level attribute
      for O_obj in entire M_mod do 
      { // Loop through all (undeleted) Objects
        if (isDeleted O_obj){continue}
        if (table     O_obj){continue}
        if (row       O_obj){continue}
        s_Key = O_obj.s_attrName
        if (s_Key == "")    {continue}
        if (find(S_attValueList, s_Key, s_Value)){s_Value = s_Value "," O_obj."Absolute Number" ""}
        else                                     {s_Value =             O_obj."Absolute Number" ""}
        put(S_attValueList, s_Key, s_Value, true)
      } // Loop through all (undeleted) Objects
    } // Object level attribute
    if (A_ad.module)
    { // Module level attribute
      s_Key   = M_mod.s_attrName
      if (s_Key != "")
      { // Attribute is not empty
        if (find(S_attValueList, s_Key, s_Value)){s_Value = s_Value "," "Module level attribute"}
        else                                     {s_Value =             "Module level attribute"}
        put(S_attValueList, s_Key, s_Value, true)
      } // Attribute is not empty
    } // Module level attribute
  } // Attribute does not exist in Module
  return S_attValueList
} // Get a list of all attribute values in the Module
// =============================================================================
void v_showAttributeValuePairs(Module M_mod1, M_mod2, string s_attrName)
{ // Search attribute value pairs in two Modules
  Skip   S_attValueListMod1 = S_getAttValueList(M_mod1, s_attrName)
  Skip   S_attValueListMod2 = S_getAttValueList(M_mod2, s_attrName)
  string s_Key, s_Value1, s_Value2
  if (!null S_attValueListMod1 && !null S_attValueListMod2)
  { // Attribute exists in both Modules
    for s_Value1 in S_attValueListMod1 do
    { // Loop through Attribute Values in Module 1
      s_Key = (string key S_attValueListMod1)
      if (find(S_attValueListMod2, s_Key, s_Value2)){print "\n" s_Key "\n\t" (name M_mod1) ": " s_Value1 "\n\t" (name M_mod2) ": " s_Value2} // Found the same attribute value in Module 2
    } // Loop through Attribute Values in Module 1
    delete S_attValueListMod1
    delete S_attValueListMod2
  } // Attribute exists in both Modules
  else
  { // Attribute does not exist in at least one of the Modules
    if (null S_attValueListMod1){print "\nAttribute '" s_attrName"' does not exist in Module '" (fullName M_mod1) "'"}
    if (null S_attValueListMod2){print "\nAttribute '" s_attrName"' does not exist in Module '" (fullName M_mod2) "'"}
  } // Attribute does not exist in at least one of the Modules
} // Search attribute value pairs in two Modules

You can use the routine like this:

Module m1 = read("<Module 1 path and name>", false, true)
Module m2 = read("<Module 1 path and name>", false, true)
close m1
close m2

Cheers,

Peter

Re: To find whether an attributeValue of 1 module exists in another module
PJAnurag - Thu Sep 13 09:22:37 EDT 2012

Thanx a lot !! this is what im looking for!

Regards
Anurag